package com.youlu.campus.admin.system;

import com.youlu.campus.entity.VO.req.SystemIpLimitQueryVO;
import com.youlu.campus.entity.system.SystemIpLimit;
import com.youlu.campus.service.system.SystemIpLimitService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
public class SystemIpLimitController {
    @Autowired
    private SystemIpLimitService systemIpLimitService;


    @PostMapping("/system/ip-limit/list")
    public QResult list(@NotNull @RequestBody SystemIpLimitQueryVO request) {
        Page<SystemIpLimit> datas = systemIpLimitService.list(request);
        return QResult.success(datas);
    }


    @PostMapping("/system/ip-limit/create")
    public QResult create(@NotNull @RequestBody SystemIpLimit request) {
        SystemIpLimit result = systemIpLimitService.create(request);
        if (StringUtils.isNotBlank(result.getId())) {
            return QResult.success(result);
        }
        return QResult.fail("创建IP限制信息失败");
    }

    @GetMapping("/system/ip-limit/delete")
    public QResult delete(@NotNull @RequestParam("id") String id) {
        boolean result = systemIpLimitService.delete(id);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("删除IP限制错误");
    }

    @PostMapping("/system/ip-limit/update")
    public QResult update(@NotNull @RequestBody SystemIpLimit request) {
        boolean result = systemIpLimitService.update(request);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("修改IP限制错误");
    }

    @GetMapping("/system/ip-limit/find-one")
    public QResult findOne(@NotNull @RequestParam("id") String id) {
        return QResult.success(systemIpLimitService.findOne(id));
    }
}
